mui.init();
var old_back = mui.back;
mui.back = function(){
	
 // var btn = ["确定","取消"];
 // mui.confirm('确认关闭当前窗口？','提示',btn,function(e){
  //  if(e.index==0){
  	//	plus.webview.currentWebview().close();
	    	var indexPage = plus.webview.getLaunchWebview();
	    	plus.storage.setItem(KeyCfg.currClickTabId,"userAccount");
	    indexPage.show();
	    indexPage.evalJS("openTab(null,subpages[2]);refreshSubPages('userAccount');"); 
//	    mui.fire(indexPage,'refresh',{});
//    }
 // });
}

//mui.back = function(){
//	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);
//	if(!targetPageId){
//		mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
//		return;
//	}
//	plus.webview.currentWebview().hide(10);
//	var targetPage = plus.webview.getWebviewById(targetPageId);
//	targetPage.show();
//  mui.fire(targetPage,'reload',{});
//  plus.storage.removeItem(KeyCfg.targetPageId);
//}

function openNote(){
	mui.alert("                  注意以下事项\n1、如果收件箱里没有收到该邮件，请查看垃圾箱，以免被误判为垃圾邮件。\n2、如果您长时间没有收到我们的激活邮件，请重新发送邮件。\n3、如果在验证过程中，出现任何问题，请致电 400-812-2468。");
}

mui.plusReady(function() {
	/*---------- 完成安全问题，下一步 提交事件 CODE START ---------*/
	var btn_finish = CmwMcore.$('btn_finish');
	MeventMgr.onTouchstart(btn_finish,function(event,self){
		var disabled = $("#btn_finish").prop("disabled");
		if(disabled) return;
		if(!valid()) return;
		submitForm();
	});
	var errMsg = null;
	var btn_sendEmail = CmwMcore.$('sendEmail');
	MeventMgr.onTouchstart(btn_sendEmail,function(event,self){
		var email_val = $("#email").val();
		if(!email_val){
			mui.alert("请输入电子邮箱!");
			return;
		}
		
		var pattern =/^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/.test(email_val);
		if(!pattern){
			mui.alert("电子邮箱格式错误，请重新填写!");
			return;
		}
		
		var url = UrlConfig.mproving_checkEmail;
		$.ajax(url, {  
	        data : {email:email_val},  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        		data = JSON.parse(data);
	        		var success = data.success;
	        		var ele = $("#btn_finish");
	        		if(!success){
	        			errMsg = "您输入的电子邮箱已经存在!";
	        			mui.alert(errMsg);
	        			ele.prop("disabled", true); 
	        		}else{
	        			errMsg = null;
	        			ele.prop("disabled", false);
	        			sendEmail();
	        		}
	        },
	        error :function(){
	        		$("#btn_finish").prop("disabled", true); 
	        }
	    });
	});


	/**
	 * 发送邮件 
	 */
	function sendEmail(){
		var email_val = $("#email").val();
		var url = UrlConfig.msms_activateemail;
		var dis=$("#sendEmail").attr("disabled")//判断是否有这属性
		if(dis)return;
		$("#sendEmail").html("邮件发送中...");
		$("#sendEmail").css('opacity',0.5);
		$("#sendEmail").prop("disabled",true);
		$.ajax(url, {  
	        data : {email:email_val},  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        	data = JSON.parse(data);
	        	var success = data.success;
	        	var msg = data.msg;
	        	var ele = $("#btn_finish");
        		if(msg==1){
	     			fun_timedownn(120);
	     			errMsg = null;
	     			mui.alert("邮箱验证码发送成功，请登录您的邮箱查看!");
	     		}else if(msg==0){
	     			$("#sendEmail").html("重新发送");
					$("#sendEmail").css('opacity',1);
					$("#sendEmail").prop("disabled", false);
	     			errMsg = "邮箱验证码发送不成功！请保证您填写的邮箱地址存在!";
	     			mui.alert(errMsg);
	     		}else if(msg==2){
	     			$("#sendEmail").html("重新发送");
					$("#sendEmail").css('opacity',1);
					$("#sendEmail").prop("disabled", false);
	     			errMsg = "发送频率太高。请2分钟后再发送！";
	     			mui.alert(errMsg);
	     		}else if(msg==3){
	     			$("#sendEmail").html("重新发送");
					$("#sendEmail").css('opacity',1);
					$("#sendEmail").prop("disabled", false);
	     			errMsg = "发送失败。请联系管理员！";
	     			mui.alert(errMsg);
	     		}
	        },
	        error :function(){
        			errMsg = "发送失败。请联系管理员！";
        			mui.alert(errMsg);
	        }
	    });
	}
	
	function valid(){
		if(errMsg){
			mui.alert(errMsg);
			return false;
		}
		var email_val = $("#email").val();
		var emailvalidcode_val = $("#emailvalidcode").val();
		if(!email_val){
			mui.alert("请输入电子邮箱!");
			return false;
		}
		
		var pattern =/^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/.test(email_val);
		if(!pattern){
			mui.alert("电子邮箱格式错误，请重新填写!");
			return false;
		}
		
		if(!emailvalidcode_val){
			mui.alert("请输入验证码!");
			return false;
		}
		return true;
	}
	
	function submitForm(){
		CmwMcore.disabled('btn_finish');
		var emailvalidcode =$("#emailvalidcode").val();
		if(!emailvalidcode){
			mui.alert("请输入验证码!");
			return false;
		}
		var formDatas = $("#theForm").serialize();
		var url = UrlConfig.mproving_saveEmail;
		$.ajax(url, {  
	        data : formDatas,  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        		data = JSON.parse(data);
	        		callback(data);
	        		CmwMcore.enabled('btn_finish');
	        },
	        error :function(){
	        		CmwMcore.enabled('btn_finish');
	        }
	    });
	}


	function callback(data){
		var success = data.success;
		var msg = data.msg;
		if(!success){
			mui.alert(msg);
			return;
		}
		forward(data);
	}
	
	function forward(data){ 
		mui.back();
//  		closeWebviews();
//  		gotoPage();
//  		plus.webview.currentWebview().close(plus.webview.AnimationTypeClose);
	}
	
	function gotoPage(){
		var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);
		if(!targetPageId){
			mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
			return;
		}
		plus.webview.currentWebview().hide(10);
		var targetPage = plus.webview.getWebviewById(targetPageId);
		targetPage.show();
	    mui.fire(targetPage,'reload',{});
	    plus.storage.removeItem(KeyCfg.targetPageId);
	}
	
	
	function closeWebviews(){
		closeWebview("validRegisterId");
		closeWebview("srealNameId");
		closeWebview("sproblemId");
	}
	function closeWebview(itemKey){
		console.log("itemKey="+itemKey);
		if(!itemKey){
			mui.alert("itemKey 不能为空!");
			return;
		}
		var webviewId = plus.storage.getItem(itemKey);		
		var webviewObj = plus.webview.getWebviewById(webviewId);
		console.log("webviewObj="+webviewObj);
		if(!webviewObj) return;
		webviewObj.close(plus.webview.AnimationTypeClose);
		console.log("======================");
	}
	/*---------- 完成安全问题，下一步 提交事件 CODE END ---------*/
	
});

function fun_timedownn(time)  {
    if(!time && time !== 0) time = 120;
    $("#sendEmail").html(time+"秒");
    time = time-1;
    if(time>=0){
    	$("#sendEmail").css('opacity',0.5);
		$("#sendEmail").prop("disabled",true);
        setTimeout("fun_timedownn("+time+")",1000);
    }else {
    	 $("#sendEmail").html("获取邮件验证码");
    	 $("#sendEmail").css('opacity',1);
		$("#sendEmail").prop("disabled", false);
    }
}